package com.huluobo.lc.ppjoke.ui.home;

import android.content.Context;
import android.view.View;

import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;

import com.huluobo.lc.libcommon.AppGlobals;
import com.huluobo.lc.libnetwork.ApiResponse;
import com.huluobo.lc.libnetwork.ApiService;
import com.huluobo.lc.libnetwork.JsonCallback;
import com.huluobo.lc.ppjoke.model.Comment;
import com.huluobo.lc.ppjoke.model.Feed;
import com.huluobo.lc.ppjoke.model.User;
import com.huluobo.lc.ppjoke.ui.login.UserManager;
import com.huluobo.lc.ppjoke.ui.view.ShareDialog;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;

/**
 * @author Lc
 * @description:
 * @date :2020/4/3 8:44
 */
public class InteractionPresenter {
    private static final String URL_TOGGLE_FEED_LIKE = "/ugc/toggleFeedLike";
    private static final String URL_TOGGLE_FEED_DISS = "/ugc/dissFeed";
    private static final String URL_SHARE = "/ugc/increaseShareCount";
    private static final String URL_TOGGLE_COMMENT_LIKE = "/ugc/toggleCommentLike";

    public static void toggleFeedLike(LifecycleOwner owner, Feed feed) {
        if (!UserManager.getInstance().isLogin()) {
            LiveData<User> loginLiveData =
                    UserManager.getInstance().login(AppGlobals.getApplication());
            loginLiveData.observe(owner, new Observer<User>() {
                @Override
                public void onChanged(User user) {
                    if (user != null) {
                        toggleFeedLikeInternal(feed);
                    }
                    loginLiveData.removeObserver(this);
                }
            });
            return;
        }
        toggleFeedLikeInternal(feed);
    }

    private static void toggleFeedLikeInternal(Feed feed) {
        ApiService.get(URL_TOGGLE_FEED_LIKE).addParam("userId",
                UserManager.getInstance().getUserId()).addParam("itemId", feed.itemId).execute(new JsonCallback<com.alibaba.fastjson.JSONObject>() {
            @Override
            public void onSuccess(ApiResponse<com.alibaba.fastjson.JSONObject> response) {
                if (response.body != null) {
                    boolean hasLiked = response.body.getBooleanValue("hasLiked");
                    feed.getUgc().setHasLiked(hasLiked);
                }
            }
        });
    }

    public static void toggleFeedDiss(LifecycleOwner owner, Feed feed) {
        if (!UserManager.getInstance().isLogin()) {
            LiveData<User> loginLiveData =
                    UserManager.getInstance().login(AppGlobals.getApplication());
            loginLiveData.observe(owner, new Observer<User>() {
                @Override
                public void onChanged(User user) {
                    if (user != null) {
                        toggleFeedDissInternal(feed);
                    }
                    loginLiveData.removeObserver(this);
                }
            });
            return;
        }
        toggleFeedDissInternal(feed);
    }

    private static void toggleFeedDissInternal(Feed feed) {
        ApiService.get(URL_TOGGLE_FEED_DISS).addParam("userId",
                UserManager.getInstance().getUserId()).addParam("itemId", feed.itemId).execute(new JsonCallback<com.alibaba.fastjson.JSONObject>() {
            @Override
            public void onSuccess(ApiResponse<com.alibaba.fastjson.JSONObject> response) {
                boolean hasLiked = response.body.getBooleanValue("hasLiked");
                feed.getUgc().setHasdiss(hasLiked);
            }
        });
    }


    public static void openShare(Context context, Feed feed) {
        String url = "http://h5.aliyun.ppjoke.com/item/%s?itemstamp=%s&user_id=%s";
        String format = String.format(url, feed.itemId, new Date().getTime(),
                UserManager.getInstance().getUserId());
        ShareDialog shareDialog = new ShareDialog(context);
        shareDialog.setShareContent(format);
        shareDialog.setShareItemsClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ApiService.get(URL_SHARE).addParam("itemId", feed.itemId).execute(new JsonCallback<JSONObject>() {
                    @Override
                    public void onSuccess(ApiResponse<JSONObject> response) {
                        if (response.body != null) {
                            try {
                                int count = response.body.getInt("count");
                                feed.getUgc().setShareCount(count);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
            }
        });
    }

    public static void toggleCommentLike(LifecycleOwner owner, Comment comment) {
        if (!UserManager.getInstance().isLogin()) {
            LiveData<User> liveData = UserManager.getInstance().login(AppGlobals.getApplication());
            liveData.observe(owner, new Observer<User>() {
                @Override
                public void onChanged(User user) {
                    liveData.removeObserver(this);
                    if (user != null) {
                        toggleCommentLikeInternal(comment);
                    }
                }
            });
            return;
        }
        toggleCommentLikeInternal(comment);

    }

    public static void toggleCommentLikeInternal(Comment comment) {
        ApiService.get(URL_TOGGLE_COMMENT_LIKE).addParam("commentId", comment.commentId).addParam("userId", UserManager.getInstance().getUserId()).execute(new JsonCallback<com.alibaba.fastjson.JSONObject>() {
            @Override
            public void onSuccess(ApiResponse<com.alibaba.fastjson.JSONObject> response) {
                if (response.body != null) {
                    boolean hasLiked = response.body.getBooleanValue("hasLiked");
                    comment.getUgc().setHasLiked(hasLiked);
                }
            }
        });
    }

}
